package com.highspeed.bdk.service.physical;

import com.bianmaba.spring.data.jpa.domain.Result;
import com.highspeed.bdk.bean.entity.ViewPostion;
import com.highspeed.bdk.repository.physical.ViewPositionRepository;
import com.highspeed.bdk.service.base.BdkBaseService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenjiabin on 2018/3/23.
 */
@Service
@Transactional
public class ViewPositionService extends BdkBaseService<ViewPostion, Integer> implements IViewPositionService {
    private static final Log LOG = LogFactory.getLog(ViewPositionService.class);
    @Autowired
    private ViewPositionRepository viewPositionRepository;

    @Override
    public ViewPositionRepository getRepository() {
        return viewPositionRepository;
    }

    @Override
    public Result save(Integer topologyId, String topologyTypeName, List<ViewPostion> entities) {
        Result result = new Result(false);

        if (topologyId == null) {
            result.setResult("参数“topologyId”为必选，不能为空");
            return result;
        }
        if (topologyTypeName == null) {
            result.setResult("参数“topologyTypeName”为必选，不能为空");
            return result;
        }
        Map<String, Object> params = new HashMap<>(0);
        params.put("topologyId", topologyId);
        params.put("topologyTypeName", topologyTypeName);
        List<ViewPostion> viewPositions = queryToList(params, null);
        getRepository().deleteInBatch(viewPositions);
        if (entities != null && !entities.isEmpty()) {
            List<ViewPostion> viewPostions = super.save(entities);
            result.setData(viewPostions);
        }
        return result;
    }
}
